<?php

namespace app\common\letterbusiness;

use app\common\mysql\LetterUsers as usersMysql;
use \think\Request;
use app\common\letterbusiness\User as userBn;
use think\Db;
class Letter extends \app\common\letterbusiness\AbstractModel {

    /**
     * 信封列表
     *
     * */
    public static function letterList($post) {
        $where=self::_getWhere($post);
        $data = usersMysql::name('letter_imgs')
                ->alias('l')
                ->field('l.*')
                ->join('letter_read_log r','r.letter_id=l.id','left')
                ->order('see_count desc')
                ->group('l.id')
                ->select();
        foreach($data as $key=>$val){
            $data[$key]['photo_imgs']=json_decode($val['photo_imgs'],true);
            foreach($data[$key]['photo_imgs'] as $k=>$v){
                $data[$key]['photo_imgs'][$k]['url']=config('app.staticResourceHost').$v['url'];
            }
            $post['letter_id']              =$val['id'];
            $data[$key]['image']=config('app.staticResourceHost').$val['image'];
            $data[$key]['seestatus'] =self::seestatus($post);
        }
        return $data;
    }

    /**
     * 获取where条件
     *
     * @param array $params
     * @return array
     */
    protected static function _getWhere($params) {
        $where = array();
        if (isset($params["user_id"]) && !empty($params['user_id'])) {
            $where['user_id'] = $params["user_id"];
        }
        if (isset($params["id"]) && !empty($params['id'])) {
            $where['id'] = $params["id"];
        }
        if (isset($params["log_id"]) && !empty($params['log_id'])) {
            $where['log_id'] = $params["log_id"];
        }
        if (isset($params["letter_id"]) && !empty($params['letter_id'])) {
            $where['letter_id'] = $params["letter_id"];
        }
        return $where;
    }
    /**
     * 信封信息
     *
     * */
    public static function letterinfo($post,$user_id) {
        $where=self::_getWhere($post);
        $data = usersMysql::name('letter_imgs')
            ->where($where)
            ->find();
        if(empty($data)){
            exception('无此信封', 10000003);
        }
        $letter['letter_id']=$post['id'];
        //第几个观看
        $seearray=Db::query("SELECT u.* FROM ( SELECT *, (@rowNum :=@rowNum + 1) AS rowNo FROM clt_letter_read_log, (SELECT(@rowNum := 0)) b ORDER BY
  clt_letter_read_log.log_time desc  ) u WHERE letter_id=".$post['id']." AND u.user_id = ".$user_id."");

        $numcount=isset($seearray[0]['rowNo'])?$seearray[0]['rowNo']:rand(52,67);
        $data['image']=config('app.staticResourceHost').$data['image'];
        $data['photo_imgs']=json_decode($data['photo_imgs'],true);
        foreach($data['photo_imgs'] as $k=>$v){
            $data['photo_imgs'][$k]['url']=config('app.staticResourceHost').$v['url'];
        }
        $data['photo_imgs']=array_values($data['photo_imgs']);
        $data['see_count']=2512+$numcount;
        $allseeletter = usersMysql::name('letter_imgs')
            ->alias('l')
            ->field('l.*,count(r.log_id) as readnums')
            ->join('letter_read_log r','r.letter_id=l.id','left')
            ->order('see_count desc')
            ->group('l.id')
            ->column('id');
        if($post['id']==$allseeletter[0]){
            $letter['user_id']=$user_id;

            if(!usersMysql::name('letter_read_log')->where($letter)->find()){
                $letter['log_time']=time();
                usersMysql::name('letter_read_log')
                    ->insertGetId($letter);
            }
            return $data;
        }else{
            $seeletter = usersMysql::name('letter_imgs')
                ->alias('l')
                ->field('l.*,count(r.log_id) as readnums')
                ->join('letter_read_log r','r.letter_id=l.id','left')
                ->where(array('user_id'=>$user_id))
                ->order('see_count desc')
                ->group('l.id')
                ->column('id');
            if(in_array($post['id'], $seeletter)){
                return $data;
            }else{
                $readnum=userBn::apiUserInfo($user_id)['readnum'];
                if($readnum<=0){
                    exception('炫耀一下', 10000003);
                }
                //比较两个间的差值
                $diff=array_values(array_diff_key($allseeletter,$seeletter));

                //要查看的是否是排在最新的，那么就需要写记录
                if($diff[0]==$post['id']){
                    $letter['user_id']=$user_id;
                    $letter['log_time']=time();
                    $letter['letter_id']=$post['id'];
                    usersMysql::name('letter_read_log')
                        ->insertGetId($letter);

                    return $data;
                }else{
                    exception('请先观看上一个', 10000003);
                }

            }

        }

    }
    /**
     * 分享信封
     *
     * */
    public static function shareinfo($post) {
        $where=self::_getWhere($post);
        return usersMysql::name('letter_imgs')
            ->field('share_title,share_img')
            ->where($where)
            ->find();
    }
	   /**
     * 分享信封
     *
     * */
    public static function shareinfolist($post) {
        $where=self::_getWhere($post);
        return usersMysql::name('letter_imgs')
            ->field('share_title,share_img')
           
            ->select();
    }
    /**
     * 观看状态
     *
     * */
    public static function seestatus($post) {
        $where=self::_getWhere($post);
        return usersMysql::name('letter_read_log')
                ->where($where)
                ->where($where)
                ->count('log_id');
    }
    /**
     * 分享信封
     *
     * */
    public static function lettershare($post) {

        $readnum=userBn::apiUserInfo($post['user_id'])['readnum'];
        if($readnum<=0){
            $where=self::_getWhere($post);
            $data = usersMysql::name('letter_share_log')
                ->where($where)

                ->find();
           if(!$data){
               $letter['user_id']=$post['user_id'];
               $letter['log_time']=time();
               $letter['letter_id']=$post['letter_id'];
               usersMysql::name('letter_share_log')
                   ->insert($letter);
           }
        }
        return true;
    }
    /**
     * 观看状态
     *
     * */
    public static function save($post,$id) {
        $post['photo_imgs']=json_encode($post['photo_imgs']);
        if($id){
            usersMysql::name('letter_imgs')
                ->where(array('id'=>$id))
                ->update($post);
        }else{
            usersMysql::name('letter_imgs')

                ->insert($post);
        }

        return true;
    }

}
